<?php

class Model_ImoreHilite extends Model {

    function __construct() {
        $this->Lookup = getModel('Model_Lookup');
    }

    function getList() {
        $products_ids = "";
        $agearr = array();
        $age = $this->Lookup->getLookup('agegroup');
        if ($age['result']) {
            foreach ($age['result'] as $value) {
                $agearr[$value['id']] = $value['name'];
            }
        }

        $product_reports_well_arr = array();
        $product_reports_well = $this->Lookup->getLookup('product_reports_well');
        if ($product_reports_well['result']) {
            foreach ($product_reports_well['result'] as $value) {
                $product_reports_well_arr[$value['id']] = $value['cname'];
            }
        }



        $param = array();
        $param['field'] = "product";
        $param['where'] = "status=1";
        $param['order'] = "priority desc,id desc";
        $param['group'] = "";
        $param['limit'] = 9;
        $param['pagesize'] = 9;
        $param['page'] = 1;
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('imore_trial_report_hilite', $param);

        $new_product_array = array();
        if ($data['result']) {
            foreach ($data['result'] as $key => $value) {

                $where = "status=1 and id =" . $value['product'];
                $param = array();
                $param['field'] = "";
                $param['where'] = $where;
                $param['order'] = "";
                $param['group'] = "";
                $param['limit'] = 1;
                $param['pagesize'] = "";
                $param['page'] = "";
                $param['join'] = "";
                $param['on'] = "";
                $data_NewProduct = db_query_hash('product', $param);
                
                if ($data_NewProduct['result']) {
                    $valueProduct = $data_NewProduct['result'][0];
                    $valueProduct['cover'] = DOMAIN . showphoto($valueProduct['cover'], 100, 100);

                    $param = array();
                    $param['field'] = "";
                    $param['where'] = "oid=" . $valueProduct['id'];
                    $param['order'] = "";
                    $param['group'] = "";
                    $param['limit'] = 1;
                    $param['pagesize'] = "";
                    $param['page'] = "";
                    $param['join'] = "";
                    $param['on'] = "";
                    $data_stat = db_query_hash('product_reports_stat', $param);

                    $reports_count = array();
                    if ($data_stat['result']) {
                        $reports_count['product'] = $valueProduct['id'];
                        $reports_count['n'] = $data_stat['result'][0]['well_score_num1'] + $data_stat['result'][0]['well_score_num2'];
                        $reports_count['n1'] = $data_stat['result'][0]['well_score_num3'];
                        $reports_count['n2'] = $data_stat['result'][0]['well_score_num4'] + $data_stat['result'][0]['well_score_num5'];
                    }

                    $valueProduct['reports_count'] = $reports_count;
                    $new_product_array[$valueProduct['id']]['details'] = $valueProduct;

                    $scores = array();
                    $scores['well_good'] = $reports_count['n'];
                    $scores['well_score_num3'] = $reports_count['n1'];
                    $scores['well_bad'] = $reports_count['n2'];
                    $new_product_array[$valueProduct['id']]['scores'] = $scores;

                    $param = array();
                    $param['field'] = "TB1.*,TB2.username,TB2.photo as m_photo";
                    $param['where'] = "oid=" . $valueProduct['id'];
                    $param['order'] = "addtime desc";
                    $param['group'] = "";
                    $param['limit'] = 3;
                    $param['pagesize'] = "";
                    $param['page'] = "";
                    $param['join'] = "left join member_imore";
                    $param['on'] = "TB1.mid = TB2.id";
                    $data_report = db_query_hash('product_reports', $param);

                    if ($data_report['result']) {
                        foreach ($data_report['result'] as $keyReport => $valueReport) {
                            $valueReport['m_photo'] = $valueReport['m_photo'] ? substr($valueReport['m_photo'],0,4)=="http"?$valueReport['m_photo']:IMOREDOMAIN.$valueReport['m_photo'] : "http://www.imore.hk/upload/avatar/default.jpg";
                            $valueReport['type'] = $valueProduct['member_type'];
                            $valueReport['item']['agegroup'] = $valueReport['agegroup'];
                            $valueReport['item']['bodygroup'] = $valueReport['body'];
                            $valueReport['item']['age'] = $agearr[$valueReport['agegroup']];
                            $valueReport['item']['well'] = $product_reports_well_arr[$valueReport['well']];

                            $data_report['result'][$keyReport] = $valueReport;
                        }
                    }
                    $new_product_array[$valueProduct['id']]['reports'] = $data_report['result'];
                }
            }
        }

        return $new_product_array;
    }

}

?>